Socket
Socket
Sign inDemoInstall

@fastify/error

Package Overview
Dependencies
0
Maintainers
18
Versions
9
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

@fastify/error


Version published
Weekly downloads
1.5M
decreased by-0.31%
Maintainers
18
Created
Weekly downloads
 

Package description

What is @fastify/error?

The @fastify/error package is designed to provide a simple and efficient way to create custom errors in Fastify applications. It allows developers to define error constructors with default status codes and messages, making error handling more consistent and streamlined across the application.

What are @fastify/error's main functionalities?

Creating custom errors

This feature allows developers to create custom error constructors. The example demonstrates how to create a 'NotFoundError' with a default message 'Resource not found' and a status code of 404.

const createError = require('@fastify/error');
const NotFoundError = createError('NOT_FOUND', 'Resource not found', 404);
throw new NotFoundError();

Custom error properties

This feature enables adding custom properties to errors. In the example, a 'DatabaseError' is created with an additional 'code' property, which is specified when the error is thrown.

const createError = require('@fastify/error');
const DatabaseError = createError('DB_ERROR', 'Database operation failed', 500, (opts) => ({ code: opts.code }));
throw new DatabaseError({ code: 'ER_NO_SUCH_TABLE' });

Other packages similar to @fastify/error

Readme

Source

@fastify/error

CI NPM version js-standard-style

A small utility, used by Fastify itself, for generating consistent error objects across your codebase and plugins.

Install

npm i @fastify/error

Usage

The module exports a function that you can use for consistent error objects, it takes 4 parameters:

createError(code, message [, statusCode [, Base]])
  • code (string, required) - The error code, you can access it later with error.code. For consistency, we recommend prefixing plugin error codes with FST_
  • message (string, required) - The error message. You can also use interpolated strings for formatting the message.
  • statusCode (number, optional) - The status code that Fastify will use if the error is sent via HTTP.
  • Base (ErrorConstructor, optional) - The base error object that will be used. (eg TypeError, RangeError)
const createError = require('@fastify/error')
const CustomError = createError('ERROR_CODE', 'Hello')
console.log(new CustomError()) // error.message => 'Hello'

How to use an interpolated string:

const createError = require('@fastify/error')
const CustomError = createError('ERROR_CODE', 'Hello %s')
console.log(new CustomError('world')) // error.message => 'Hello world'

How to add cause:

const createError = require('@fastify/error')
const CustomError = createError('ERROR_CODE', 'Hello %s')
console.log(new CustomError('world', {cause: new Error('cause')})) 
// error.message => 'Hello world'
// error.cause => Error('cause')

TypeScript

It is possible to limit your error constructor with a generic type using TypeScript:

const CustomError = createError<[string]>('ERROR_CODE', 'Hello %s')
new CustomError('world')
//@ts-expect-error
new CustomError(1)

License

Licensed under MIT.

Keywords

FAQs

Last updated on 04 Nov 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc